<template>
	<div class='login container'>
		<Header></Header>
		<section>
			<div class='login-tel'>
				<input type="text" v-model="userTel" placeholder="请输入手机号" pattern="[0-9]*">
			</div>
			<div class='login-code'>
				<input type="text" v-model="userPwd" placeholder="请输入密码" >
			</div>
			<div class='login-btn' @click="login">登 录</div>
			<div class='tab'>
				<span @click='goLogin'>短信登录</span>
				<span @click="goRecovery">找回密码</span>
				<span @click="$router.push('/register')">快速注册</span>
			</div>
		</section>
		<Tabbar></Tabbar>
	</div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue'
import Header from './Header'
import { Toast } from 'mint-ui';
import http from '@/common/api/request.js'
import {mapMutations} from 'vuex'
export default{
	components:{
		Header,
		Tabbar
	},
	data(){
		return{
			userTel:'',//用户输入的手机号
			userPwd:'',//用户输入的密码
			relus:{
				// 手机验证
				userTel:{
					rule:/^1[3-9]\d{9}$/,
					msg:'手机号不能为空,并且是11位数据'
				},
				// 密码验证
				userPwd:{
					rule:/^\w{6,12}$/,
					msg:'密码不能为空,并且要求6-12位'
				},
			}
		}
	},
	methods:{
		...mapMutations(['userLogin']),
		// 登录
		login(){
			// 验证
			if(!this.validate('userTel')) return
			if(!this.validate('userPwd')) return

			// 发送请求
			http.$axios({
				url:'/api/login',
				method:'POST',
				data:{
					userTel:this.userTel,
					userPwd:this.userPwd
				}
			}).then(res=>{
				// 弹出提示框
				Toast(res.msg)
				// 如果登录失败
				if(!res.success) return

				// 如果登录成功---跳转页面，存储用户信息
				console.log(res.data)
				this.userLogin(res.data)
				// 跳转我的主页
				this.$router.push('my')
			})
		},

		// 跳转登录
		goLogin(){
			this.$router.push('/Login');
		},
		// 跳转找回密码
		goRecovery(){
			this.$router.push('/recovery')
		},
		// 验证
		validate(key){
	
			let bool=true
			if(!this.relus[key].rule.test(this[key])){
				//提示信息
				Toast(this.relus[key].msg)
				bool=false 
				// return false
			}
				return bool
		}
	}
}
</script>

<style scoped lang='scss'>
section{
	display: flex;
	flex-direction: column;
	align-items: center;
	background-color:#f5f5f5;
	div{
		margin:0.266666rem 0;
		width: 8.933333rem;
		height: 1.173333rem;
	}
	input{
		box-sizing: border-box;
		padding:0 0.266666rem;
		line-height: 1.173333rem;
		background-color: #FFFFFF;
		border:1px solid #ccc;
		border-radius: 6px;
		font-size:0.32rem;
	}
	.login-tel{
		margin-top:0.8rem;
		input{
			width: 8.933333rem;
		}
	}
	.login-code{
		display: flex;
		input{
			flex:1;
		}
	}
	.login-btn{
		line-height: 44px;
		color:#fff;
		text-align: center;
		background-color: #b0352f;
		border-radius: 6px;
		font-size: 0.5rem;
	}
	.tab{
		display: flex;
		justify-content: space-between;
		font-size:0.373333rem;
	}
}
</style>
